package ictbk.ttmk51.grapro.app.utils;

import ictbk.ttmk51.grapro.app.safephonebook.Address;
import ictbk.ttmk51.grapro.app.safephonebook.Contact;
import ictbk.ttmk51.grapro.app.safephonebook.Email;
import ictbk.ttmk51.grapro.app.safephonebook.IM;
import ictbk.ttmk51.grapro.app.safephonebook.Organization;
import ictbk.ttmk51.grapro.app.safephonebook.Phone;
import ictbk.ttmk51.grapro.app.safesms.SMS;

import java.io.Reader;
import java.util.ArrayList;

import android.util.Log;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class MyXStream {
	public static String ContactListtoXML(ArrayList<Contact> list) {
		// ClientContact co chua thuoc tinh Bitmap nen khong the tao XML duoc
		// Can phai chuyen cac doi tuong ClientContact thanh cac doi tuong
		// Contact

		XStream writer = new XStream();
		writer.alias("contact", Contact.class);
		writer.aliasAttribute(Contact.class, "checked", "checked");
		writer.aliasAttribute(Contact.class, "id", "ID");
		writer.aliasAttribute(Contact.class, "displayName", "name");
		writer.aliasAttribute(Contact.class, "stringPhoto", "stringPhoto");
		writer.aliasField("notes", Contact.class, "notes");
		
		writer.alias("phone", Phone.class);
		writer.aliasAttribute(Phone.class, "number", "value");
		writer.aliasAttribute(Phone.class, "type", "type");
		
		writer.alias("email", Email.class);
		writer.aliasAttribute(Email.class, "address", "value");
		writer.aliasAttribute(Email.class, "type", "type");
		
		writer.alias("address", Address.class);
		writer.aliasAttribute(Address.class, "poBox", "pobox");
		writer.aliasAttribute(Address.class, "street", "street");
		writer.aliasAttribute(Address.class, "city", "city");
		writer.aliasAttribute(Address.class, "state", "state");
		writer.aliasAttribute(Address.class, "postalCode", "postal_code");
		writer.aliasAttribute(Address.class, "country", "country");
		writer.aliasAttribute(Address.class, "type", "type");
		
		writer.alias("IM", IM.class);
		writer.aliasAttribute(IM.class, "name", "name");
		writer.aliasAttribute(IM.class, "type", "type");
		
		writer.alias("organization", Organization.class);
		writer.aliasAttribute(Organization.class, "organization", "org");
		writer.aliasAttribute(Organization.class, "positions", "pos");
		writer.aliasAttribute(Organization.class, "type", "type");
		
		return writer.toXML(list);
		
	}

	@SuppressWarnings("unchecked")
	public static ArrayList<Contact> getContactListfromXML(String xml) {
		XStream reader = new XStream(new DomDriver());
		reader.alias("contact", Contact.class);
		reader.aliasAttribute(Contact.class, "checked", "checked");
		reader.aliasAttribute(Contact.class, "id", "ID");
		reader.aliasAttribute(Contact.class, "displayName", "name");
		reader.aliasAttribute(Contact.class, "stringPhoto", "stringPhoto");
		reader.aliasField("notes", Contact.class, "notes");
		
		reader.alias("phone", Phone.class);
		reader.aliasAttribute(Phone.class, "number", "value");
		reader.aliasAttribute(Phone.class, "type", "type");
		
		reader.alias("email", Email.class);
		reader.aliasAttribute(Email.class, "address", "value");
		reader.aliasAttribute(Email.class, "type", "type");
		
		reader.alias("address", Address.class);
		reader.aliasAttribute(Address.class, "poBox", "pobox");
		reader.aliasAttribute(Address.class, "street", "street");
		reader.aliasAttribute(Address.class, "city", "city");
		reader.aliasAttribute(Address.class, "state", "state");
		reader.aliasAttribute(Address.class, "postalCode", "postal_code");
		reader.aliasAttribute(Address.class, "country", "country");
		reader.aliasAttribute(Address.class, "type", "type");
		
		reader.alias("IM", IM.class);
		reader.aliasAttribute(IM.class, "name", "name");
		reader.aliasAttribute(IM.class, "type", "type");
		
		reader.alias("organization", Organization.class);
		reader.aliasAttribute(Organization.class, "organization", "org");
		reader.aliasAttribute(Organization.class, "positions", "pos");
		reader.aliasAttribute(Organization.class, "type", "type");

		return (ArrayList<Contact>) reader.fromXML(xml);
	}

	@SuppressWarnings("unchecked")
	public static ArrayList<Contact> getContactListfromXML(Reader xml) {
		XStream reader = new XStream(new DomDriver());
		reader.alias("contact", Contact.class);
		reader.aliasAttribute(Contact.class, "checked", "checked");
		reader.aliasAttribute(Contact.class, "id", "ID");
		reader.aliasAttribute(Contact.class, "displayName", "name");
		reader.aliasAttribute(Contact.class, "stringPhoto", "stringPhoto");
		reader.aliasField("notes", Contact.class, "notes");
		
		reader.alias("phone", Phone.class);
		reader.aliasAttribute(Phone.class, "number", "value");
		reader.aliasAttribute(Phone.class, "type", "type");
		
		reader.alias("email", Email.class);
		reader.aliasAttribute(Email.class, "address", "value");
		reader.aliasAttribute(Email.class, "type", "type");
		
		reader.alias("address", Address.class);
		reader.aliasAttribute(Address.class, "poBox", "pobox");
		reader.aliasAttribute(Address.class, "street", "street");
		reader.aliasAttribute(Address.class, "city", "city");
		reader.aliasAttribute(Address.class, "state", "state");
		reader.aliasAttribute(Address.class, "postalCode", "postal_code");
		reader.aliasAttribute(Address.class, "country", "country");
		reader.aliasAttribute(Address.class, "type", "type");
		
		reader.alias("IM", IM.class);
		reader.aliasAttribute(IM.class, "name", "name");
		reader.aliasAttribute(IM.class, "type", "type");
		
		reader.alias("organization", Organization.class);
		reader.aliasAttribute(Organization.class, "organization", "org");
		reader.aliasAttribute(Organization.class, "positions", "pos");
		reader.aliasAttribute(Organization.class, "type", "type");

		return (ArrayList<Contact>) reader.fromXML(xml);
	}
	
	public static String SMSListtoXML(ArrayList<SMS> SMSList){
		XStream writer = new XStream();
		writer.alias("SMS", SMS.class);
//		writer.aliasAttribute(SMS.class, "id", "id");
//		writer.aliasAttribute(SMS.class, "thread_id", "thread_id");
//		writer.aliasAttribute(SMS.class, "body", "body");
//		writer.aliasAttribute(SMS.class, "address", "address");
//		writer.aliasAttribute(SMS.class, "read", "read");
//		writer.aliasAttribute(SMS.class, "type", "type");
//		writer.aliasAttribute(SMS.class, "status", "status");
//		writer.aliasAttribute(SMS.class, "date", "date");
//		writer.aliasAttribute(SMS.class, "seen", "seen");
		return writer.toXML(SMSList);
	}
	
	@SuppressWarnings("unchecked")
	public static ArrayList<SMS> getSMSListfromXML(String xml){
		XStream reader = new XStream(new DomDriver());
		reader.alias("SMS", SMS.class);
//		reader.aliasAttribute(SMS.class, "id", "id");
//		reader.aliasAttribute(SMS.class, "thread_id", "thread_id");
//		reader.aliasAttribute(SMS.class, "body", "body");
//		reader.aliasAttribute(SMS.class, "address", "address");
//		reader.aliasAttribute(SMS.class, "read", "read");
//		reader.aliasAttribute(SMS.class, "type", "type");
//		reader.aliasAttribute(SMS.class, "status", "status");
//		reader.aliasAttribute(SMS.class, "date", "date");
//		reader.aliasAttribute(SMS.class, "seen", "seen");
		
		return (ArrayList<SMS>) reader.fromXML(xml);
	}
	@SuppressWarnings("unchecked")
	public static ArrayList<SMS> getSMSListfromXML(Reader xml){
		XStream reader = new XStream(new DomDriver());
		reader.alias("SMS", SMS.class);
		reader.aliasAttribute(SMS.class, "id", "id");
		reader.aliasAttribute(SMS.class, "thread_id", "thread_id");
		reader.aliasAttribute(SMS.class, "body", "body");
		reader.aliasAttribute(SMS.class, "address", "address");
		reader.aliasAttribute(SMS.class, "read", "read");
		reader.aliasAttribute(SMS.class, "type", "type");
		reader.aliasAttribute(SMS.class, "status", "status");
		reader.aliasAttribute(SMS.class, "date", "date");
		reader.aliasAttribute(SMS.class, "seen", "seen");
		
		if(reader.fromXML(xml) == null){
			Log.i("Sao thế??", "Không hiểu");
		}
		
		return (ArrayList<SMS>) reader.fromXML(xml);
	}
}
